/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view;

import com.sun.lwuit.ButtonGroup;
import com.sun.lwuit.Command;
import com.sun.lwuit.Label;
import com.sun.lwuit.RadioButton;
import com.sun.lwuit.TextArea;
import com.sun.lwuit.events.ActionEvent;
import com.sun.lwuit.layouts.BoxLayout;
import core.ScreenEngine;
import core.Settings;
import view.commands.BackCommand;

/**
 *
 * @author Kvest
 */
public class SortSettingsForm extends BasicForm
{
    private static final int ACCEPT_COMMAND_ID = 1;
    
    private RadioButton rbByTime;
    private RadioButton rbAlienBottom;
    
    public SortSettingsForm()
    {
        super("efir_sort");
        
        init();
    }
    
    private void init()
    {
        setLayout(new BoxLayout(BoxLayout.Y_AXIS));
        setCyclicFocus(false);
         
        TextArea text = new TextArea((new Label("efir_sort_text")).getText(), 1, 30);
        text.setEditable(false);
        text.setGrowByContent(true);
        text.setFocusable(false);
        addComponent(text);
  
        ButtonGroup group = new ButtonGroup();
        
        rbByTime = new RadioButton("by_time");
        rbByTime.setSelectCommandText("");
        group.add(rbByTime);
        addComponent(rbByTime);
        
        rbAlienBottom = new RadioButton("alien_bottom");
        rbAlienBottom.setSelectCommandText("");
        group.add(rbAlienBottom);
        addComponent(rbAlienBottom);
        
        addCommand(new BackCommand());
        
        Command accept = new Command("accept", ACCEPT_COMMAND_ID)
        {
            public void actionPerformed(ActionEvent evt)
            {   
                //Сохряняем изменения
                accept();
                
                //ВЫходим на главную
                ScreenEngine.getEngine().popTo(0);
                
                evt.consume();
            }
        };
        addCommand(accept);          
    }
    
    public void show()
    {
        setContent();
        
        super.show();
    }
    
    private void setContent()
    {
        switch(Settings.getSettings().getEfirSort())
        {
            case Settings.ES_BYTIME : rbByTime.setSelected(true); break;
            case Settings.ES_ALIEN_BOTTOM : rbAlienBottom.setSelected(true); break;
        }
    }
    
    private void accept()
    {
        if (rbByTime.isSelected())
        {
            Settings.getSettings().setEfirSort(Settings.ES_BYTIME);
        }
        else if (rbAlienBottom.isSelected())
        {
            Settings.getSettings().setEfirSort(Settings.ES_ALIEN_BOTTOM);
        }
        Settings.getSettings().save();
    }
}
